package com.uls.web.controller.czj;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.czj.domain.CzjBuilding;
import com.uls.czj.domain.CzjPublichousing;
import com.uls.czj.service.ICzjBuildingService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.czj.domain.CzjCommercialproperty;
import com.uls.czj.service.ICzjCommercialpropertyService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 经营用房管理Controller
 * 
 * @author czj
 * @date 2024-11-10
 */
@RestController
@RequestMapping("/czj/commercialproperty")
public class CzjCommercialpropertyController extends BaseController
{
    @Autowired
    private ICzjCommercialpropertyService czjCommercialpropertyService;
    @Autowired
    private ICzjBuildingService czjBuildingService;
    /**
     * 查询经营用房管理列表
     */
    @PreAuthorize("@ss.hasPermi('czj:commercialproperty:list')")
    @GetMapping("/list")
    public TableDataInfo list(CzjCommercialproperty czjCommercialproperty)
    {
        startPage();
        List<CzjCommercialproperty> list = czjCommercialpropertyService.selectCzjCommercialpropertyList(czjCommercialproperty);
        for (CzjCommercialproperty czjCommercialproperty1:list){
            CzjBuilding czjBuilding= czjBuildingService.selectCzjBuildingByBuildingid(czjCommercialproperty1.getBuildingid());
            czjCommercialproperty1.setBuilding(czjBuilding.getLocation()+czjBuilding.getBuildingname());
        }
        return getDataTable(list);
    }

    /**
     * 导出经营用房管理列表
     */
    @PreAuthorize("@ss.hasPermi('czj:commercialproperty:export')")
    @Log(title = "经营用房管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CzjCommercialproperty czjCommercialproperty)
    {
        List<CzjCommercialproperty> list = czjCommercialpropertyService.selectCzjCommercialpropertyList(czjCommercialproperty);
        ExcelUtil<CzjCommercialproperty> util = new ExcelUtil<CzjCommercialproperty>(CzjCommercialproperty.class);
        util.exportExcel(response, list, "经营用房管理数据");
    }

    /**
     * 获取经营用房管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('czj:commercialproperty:query')")
    @GetMapping(value = "/{commercialid}")
    public AjaxResult getInfo(@PathVariable("commercialid") Long commercialid)
    {
        return success(czjCommercialpropertyService.selectCzjCommercialpropertyByCommercialid(commercialid));
    }

    /**
     * 新增经营用房管理
     */
    @PreAuthorize("@ss.hasPermi('czj:commercialproperty:add')")
    @Log(title = "经营用房管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CzjCommercialproperty czjCommercialproperty)
    {
        CzjBuilding czjBuilding= czjBuildingService.selectCzjBuildingByBuildingid(czjCommercialproperty.getBuildingid());
        czjBuilding.setBuildingtype("3");
        czjBuildingService.updateCzjBuilding(czjBuilding);
        return toAjax(czjCommercialpropertyService.insertCzjCommercialproperty(czjCommercialproperty));
    }

    /**
     * 修改经营用房管理
     */
    @PreAuthorize("@ss.hasPermi('czj:commercialproperty:edit')")
    @Log(title = "经营用房管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CzjCommercialproperty czjCommercialproperty)
    {
        return toAjax(czjCommercialpropertyService.updateCzjCommercialproperty(czjCommercialproperty));
    }

    /**
     * 删除经营用房管理
     */
    @PreAuthorize("@ss.hasPermi('czj:commercialproperty:remove')")
    @Log(title = "经营用房管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commercialids}")
    public AjaxResult remove(@PathVariable Long[] commercialids)
    {
        for (Long l:commercialids){
            Long id= czjCommercialpropertyService.selectCzjCommercialpropertyByCommercialid(l).getBuildingid();
            CzjBuilding czjBuilding= czjBuildingService.selectCzjBuildingByBuildingid(id);
            czjBuilding.setBuildingtype("0");
            czjBuildingService.updateCzjBuilding(czjBuilding);
        }

        return toAjax(czjCommercialpropertyService.deleteCzjCommercialpropertyByCommercialids(commercialids));
    }
}
